package com.docs2swf.lib;

import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XCloseable;

import java.io.File;
import java.io.FileFilter;

import ooo.connector.BootstrapSocketConnector;


public class DocumentConverter {
	public static XComponentContext createContext() throws Exception {
	    // get the remote office component context
	    // return Bootstrap.bootstrap();
	    String oooExeFolder = "C:/Program Files (x86)/OpenOffice.org 3/program/";
	    return BootstrapSocketConnector.bootstrap(oooExeFolder);
	}
	
	public static XComponentLoader createLoader(XComponentContext context) throws Exception {
	    // get the remote office service manager
	    XMultiComponentFactory mcf = context.getServiceManager();
	    Object desktop = mcf.createInstanceWithContext("com.sun.star.frame.Desktop", context);
	    return UnoRuntime.queryInterface(XComponentLoader.class, desktop);
	}
	
	public static Object loadDocument(XComponentLoader loader, String inputFilePath) throws Exception {
	    // Preparing properties for loading the document
	    PropertyValue[] propertyValues = new PropertyValue[1];
	    propertyValues[0] = new PropertyValue();
	    propertyValues[0].Name = "Hidden";
	    propertyValues[0].Value = new Boolean(true);
	    
	    // Composing the URL by replacing all backslashs
	    File inputFile = new File(inputFilePath);
	    String inputUrl = "file:///" + inputFile.getAbsolutePath().replace('\\', '/');

	    return loader.loadComponentFromURL(inputUrl, "_blank", 0, propertyValues);
	}
	
	public static void convertDocument(Object doc, String outputFilePath, String convertType) throws Exception {
	    // Preparing properties for converting the document
	    PropertyValue[] propertyValues = new PropertyValue[2];
	    // Setting the flag for overwriting
	    propertyValues[0] = new PropertyValue();
	    propertyValues[0].Name = "Overwrite";
	    propertyValues[0].Value = new Boolean(true);
	    // Setting the filter name
	    propertyValues[1] = new PropertyValue();
	    propertyValues[1].Name = "FilterName";
	    propertyValues[1].Value = convertType;
	    
	    // Composing the URL by replacing all backslashs
	    File outputFile = new File(outputFilePath);
	    String outputUrl = "file:///" + outputFile.getAbsolutePath().replace('\\', '/');
	    
	    // Getting an object that will offer a simple way to store
	    // a document to a URL.
	    XStorable storable = UnoRuntime.queryInterface(XStorable.class, doc);
	    // Storing and converting the document
	    storable.storeToURL(outputUrl, propertyValues);
	}
	
	public static void closeDocument(Object doc) throws Exception {
	    // Closing the converted document. Use XCloseable.clsoe if the
	    // interface is supported, otherwise use XComponent.dispose
	    XCloseable closeable = UnoRuntime.queryInterface(XCloseable.class, doc);
	    
	    if (closeable != null) {
	    	closeable.close(false);
	    } else {
	        XComponent component = UnoRuntime.queryInterface(XComponent.class, doc);
	        component.dispose();
	    }
	}
	
	public static void main(String args[]) {
	    String inputFilePath = "D:/word.docx";
	    String outputFilePath = "D:/word.pdf";
	    
	    // the given type to convert to
	    String convertType = "writer_pdf_Export";
	    
	    try {
	        XComponentContext context = createContext();
	        System.out.println("connected to a running office ...");
	        
	        XComponentLoader compLoader = createLoader(context);
	        System.out.println("loader created ...");
	        
	        Object doc = loadDocument(compLoader, inputFilePath);
	        System.out.println("document loaded ...");
	        
	        convertDocument(doc, outputFilePath, convertType);
	        System.out.println("document converted ...");
	        
	        closeDocument(doc);
	        System.out.println("document closed ...");
	        
	        System.exit(0);
	    } catch (Exception e) {
	        e.printStackTrace(System.err);
	        System.exit(1);            
	    }
	}
}
